<template>
  <div class="edit-container">
    <!-- 头部 -->
    <van-nav-bar
      title="编辑资料"
      left-text="返回"
      left-arrow
      @click-left="$router.back()"
    />

    <!-- 图片 -->
    <div class="avatar">
      <van-image round width="120px" height="120px" :src="userInfo.photo" />
      <van-uploader :after-read="afterRead" />
    </div>

    <!-- 单元格 -->
    <van-cell-group>
      <van-cell title="姓名" :value="userInfo.name" is-link @click="editName" />
      <van-cell
        title="性别"
        :value="userInfo.gender ? '女' : '男'"
        is-link
        @click="showPopup = true"
      />
      <van-cell
        title="生日"
        :value="userInfo.birthday"
        is-link
        @click="
          showDate = true;
          currentDate = new Date(userInfo.birthday);
        "
      />
    </van-cell-group>

    <!-- 编辑姓名弹窗 -->
    <van-dialog
      v-model="show"
      title="修改姓名"
      show-cancel-button
      @confirm="updateName"
    >
      <van-field ref="input" v-model="value" placeholder="请输入姓名" />
    </van-dialog>

    <!-- 编辑性别弹框 -->
    <van-popup v-model="showPopup" position="bottom">
      <van-nav-bar
        title="修改性别"
        left-arrow
        @click-left="showPopup = false"
      />
      <van-cell title="男" is-link @click="updateGender(0)" />
      <van-cell title="女" is-link @click="updateGender(1)" />
    </van-popup>

    <!-- 编辑生日弹框 -->
    <van-popup v-model="showDate" position="bottom">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="mindate"
        @cancel="showDate = false"
        @confirm="updateBirthday"
      />
    </van-popup>

    <!-- 图片裁剪功能 -->
    <div class="cropper" v-show="showCropper">
      <vueCropper
        ref="cropper"
        :img="img"
        autoCrop
        autoCropWidth="240"
        autoCropHeight="240"
      ></vueCropper>
      <van-button type="primary" @click="showCropper = false">取消</van-button>
      <van-button type="info" @click="updatePhoto">确定</van-button>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { editUserInfo, editUserPhoto } from "./user.js";
import moment from "moment";
import { VueCropper } from "vue-cropper";
export default {
  name: "edit",
  data() {
    return {
      // 性别弹出框
      show: false,
      value: "",
      showPopup: false,

      // 日期弹出框
      currentDate: new Date(),
      showDate: false,
      mindate: new Date(1980, 0, 1),

      img: "",
      showCropper: false,
    };
  },
  components: {
    VueCropper,
  },
  methods: {
    // 编辑姓名弹框
    editName() {
      // 弹出dialog弹窗
      this.show = true;
      // 输入框默认要是当前用户的名字
      this.value = this.userInfo.name;

      // 输入框默认成为焦点
      this.$nextTick(() => {
        this.$refs.input.focus();
      });
    },
    // 修改名字
    updateName() {
      editUserInfo({ name: this.value }).then((ret) => {
        this.$toast.success("修改成功");
        // 更新vuex
        this.$store.commit("updateUserInfo", {
          ...this.userInfo,
          name: this.value,
        });
      });
    },

    // 修改性别
    updateGender(gender) {
      editUserInfo({ gender }).then((ret) => {
        this.$toast.success("修改成功");

        this.showPopup = false;
        // 更新vuex
        this.$store.commit("updateUserInfo", {
          ...this.userInfo,
          gender,
        });
      });
    },

    // 修改生日
    updateBirthday() {
      let birthday = moment(this.currentDate).format("YYYY-MM-DD");
      editUserInfo({ birthday: birthday })
        .then((ret) => {
          this.$toast.success("更新成功");
          this.$store.commit("updateUserInfo", {
            ...this.userInfo,
            birthday,
          });
        })
        .then(() => {
          this.showDate = false;
        });
    },

    // 弹出文件裁剪框
    afterRead(file) {
      this.img = file.content;
      this.showCropper = true;
    },
    // 上传裁剪图片
    updatePhoto() {
      // 获取截图的blob数据
      this.$refs.cropper.getCropBlob((data) => {
        let fd = new FormData();
        fd.append("photo", data);
        editUserPhoto(fd).then((ret) => {
          this.$toast.success("更新成功");
          this.showCropper = false;
          this.$store.commit("updateUserInfo", {
            ...this.userInfo,
            photo: ret.data.data.photo,
          });
        });
      });
    },
  },
  computed: {
    // 添加计算属性  userInfo = return this.$store.state.userInfo
    ...mapState(["userInfo"]),
  },
};
</script>

<style lang='less'>
.edit-container {
  .van-nav-bar__content {
    background-color: #0098fa;
    .van-nav-bar__title,
    .van-nav-bar__text,
    .van-icon {
      color: white;
    }
  }

  // 头像位置
  .avatar {
    display: flex;
    height: 150px;
    justify-content: center;
    align-content: center;

    //文件上传盒子
    .van-uploader {
      position: absolute;
      opacity: 0;
      .van-uploader__upload {
        width: 120px;
        height: 120px;
        margin: 0;
      }
    }
  }
  // 裁切弹框样式
  .cropper {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    .van-button--primary {
      position: absolute;
      left: 0;
      bottom: 0;
    }
    .van-button--info {
      position: absolute;
      right: 0;
      bottom: 0;
    }
  }
}
</style>